<?php

namespace app\admin\model\shop\order;

use app\admin\model\shop\user\Commission;
use app\admin\model\shop\user\User;
use think\facade\Db;
use think\Model;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;
use think\model\relation\HasOne;

/**
 * Order
 */
class Order extends Model
{
    use SoftDelete;

    protected $defaultSoftDelete = 0;

    // 表名
    protected $name = 'shop_order';

    protected $type = [
        'user_contact'    => 'json',
        'discount_data'   => 'json',
        'discount_detail' => 'json',
        'create_time'     => 'timestamp: Y-m-d H:i:s',
        'update_time'     => 'timestamp: Y-m-d H:i:s',
        'pay_time'        => 'timestamp: Y-m-d H:i:s',
        'delivery_time'   => 'timestamp: Y-m-d H:i:s',
        'receive_time'    => 'timestamp: Y-m-d H:i:s',
        'evaluate_time'   => 'timestamp: Y-m-d H:i:s',
        'cancel_time'     => 'timestamp: Y-m-d H:i:s',
    ];

    protected $jsonAssoc = true;

    //关联详情
    public function detail(): HasMany
    {
        return $this->hasMany(OrderDetail::class, 'order_id', 'id');
    }

    //关联用户
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    //关联地址
    public function address(): HasOne
    {
        return $this->hasOne(OrderAddress::class, 'order_id', 'id');
    }

    public function statusLog(): HasMany
    {
        return $this->hasMany(OrderStatusLog::class, 'order_id', 'id');
    }

    public function orderRefund(): HasOne
    {
        return $this->hasOne(OrderRefund::class, 'order_id', 'id');
    }

    public function delivery(): HasMany
    {
        return $this->hasMany(OrderDelivery::class, 'order_id', 'id');
    }

    public function OrderSuccess($data)
    {
        $return = ['status'=>false,'msg'=>'操作失败'];
        Db::startTrans();
        try {
            $res = $this->where('id',$data['order_id'])->update(['status'=>5]);
            if($res){
                //给陪玩分佣
                $playorder = Db::name('shop_order_play')->where('order_id',$data['order_id'])->select()->toArray();
                if(isset($playorder)){
                    foreach ($playorder as $k=>$v){
                        if($v['commission'] > 0){
                            $model = new Commission();
                            $model->change($v['user_id'],$model::TYPE_ORDER,$v['commission'],$data['order_id'],'');
                        }
                    }
                }
                //给上级分佣
                $orderinfo = $this->where('id',$data['order_id'])->find();
                $upid = Db::name('shop_user')->where('id',$orderinfo['user_id'])->find();

                if($upid['pid']){
                    $setting = Db::name('shop_user_grade')->where('id',$upid['grade_id'])->find();
                    if(isset($setting['proportion']) && $setting['proportion'] > 0){
                        $money = number_format($orderinfo['total_price'] * ($setting['proportion']/100), 2);
                        $model = new Commission();
                        $model->change($upid['pid'],$model::TYPE_DISTRIBUTION,$money,$data['order_id'],'');
                    }
                }
                Db::name('shop_order_status_log')->insert($data);
                Db::name('shop_order_play')->where('order_id',$data['order_id'])->update(['status'=>3]);
                $return['status'] = true;
                $return['msg'] = '操作成功';
                Db::commit();
            }
        }catch (\Exception $e){
            Db::rollback();
            $return['msg'] = $e->getMessage();
        }
        return $return;
    }
}